/*  ----------------------------------------------------------------------------
 *  Arello Mobile
 *  Fast 3d Engine
 *  ----------------------------------------------------------------------------
 * Except where otherwise noted, this work is licensed under
 * http://creativecommons.org/licenses/by/3.0
 */

#ifndef __STATIC_REF_COUNTED__
#define __STATIC_REF_COUNTED__

namespace liba
{

template<class Sta>
class StaticRefCounted
{
	static int ref_counter;
	static Sta * sta;
protected:
	static Sta & statics()
	{
		return *sta;
	}
public:
	StaticRefCounted()
	{
		if( ++ref_counter == 1 )
		{
			sta = new Sta;
		}
	}
	StaticRefCounted(const StaticRefCounted & src)
	{
		++ref_counter;
	}
	~StaticRefCounted()
	{
		if( --ref_counter == 0 )
		{
			delete sta; sta = 0;
		}
	}
};


template<class Sta>
int StaticRefCounted<Sta>::ref_counter = 0;

template<class Sta>
Sta * StaticRefCounted<Sta>::sta = 0;

}//namespace liba
using namespace liba;

#endif // __STATIC_REF_COUNTED__